<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>examListView</title>
    <title>课程列表</title>
    <link rel="stylesheet" type="text/css" href="/static/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/static/easyui/css/demo.css">
    <script type="text/javascript" src="/static/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/static/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/easyui/themes/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" th:inline="none">
        $(function () {
            //datagrid初始化
            var dataList = $("#dataList");
            dataList.datagrid({
                title: '课程列表',
                iconCls: 'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                url: "/schedule/list",
                idField: 'id',
                singleSelect: true,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                sortName: 'id',
                sortOrder: 'ASC',
                remoteSort: true,
                columns: [
                    [
                        {field: 'chk', checkbox: true, width: 50},
                        {field: 'id', title: 'ID', width: 50, sortable: true},
                        {field: 'name', title: '课程名称', width: 200, sortable: true},
                        {
                            field: 'clazz', title: '班级', width: 100,
                            formatter: function (value, row, index) {
                                if (row.clazz) {
                                    return row.clazz.name;
                                } else {
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'course', title: '科目名称', width: 100,
                            formatter: function (value, row, index) {
                                if (row.course) {
                                    return row.course.name;
                                } else {
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'teacher', title: '教师', width: 100,
                            formatter: function (value, row, index) {
                                if (row.course) {
                                    return row.teacher.name;
                                } else {
                                    return value;
                                }
                            }
                        },
                    ]
                ],
                toolbar: '#toolbar'
            });
            //设置分页控件
            var p = dataList.datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数，默认为10
                pageList: [10, 20, 30, 50, 100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
            //设置工具类按钮
            $("#add").click(function () {
                $("#addDialog").dialog("open");
            });


            //fengyexjtu@126.com
            //根据所选择的列 删除课程相关信息
            $("#delete").click(function () {
                var selectRow = $("#dataList").datagrid("getSelected");
                if (selectRow == null) {
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else {
                    var id = selectRow.id;
                    $.messager.confirm("消息提醒", "将删除 " + selectRow.name + " 的数据，确认继续？", function (r) {
                        if (r) {
                            $.ajax({
                                type: "post",
                                url: "/schedule/delete",
                                data: {id: id},
                                success: function (msg) {
                                    if (msg == "success") {
                                        $.messager.alert("消息提醒", "删除成功!", "info");
                                        //刷新表格
                                        dataList.datagrid("reload");
                                        dataList.datagrid("uncheckAll");
                                    } else {
                                        $.messager.alert("消息提醒", "删除失败!", "warning");
                                        return;
                                    }
                                }
                            });
                        }
                    });
                }
            });
            //成绩统计
            $("#escore").click(function () {

                var exam = $("#dataList").datagrid("getSelected");
                if (exam == null) {
                    $.messager.alert("消息提醒", "请选择考试进行统计!", "warning");
                } else {
                    var data = {
                        id: exam.id,
                        gradeid: exam.gradeid,
                        clazzid: exam.clazzid,
                        courseid: exam.courseid,
                        type: exam.type
                    };

                    //动态显示该次考试的科目
                    $.ajax({
                        type: "post",
                        url: "ScoreServlet?method=ColumnList",
                        data: data,
                        dataType: "json",
                        async: false,
                        success: function (result) {
                            console.log(result);
                            var columns = [];
                            $.each(result, function (i, course) {
                                var column = {};
                                column["field"] = "course" + course.id;
                                column["title"] = course.name;
                                column["width"] = 70;
                                column["resizable"] = false;
                                column["sortable"] = true;

                                columns.push(column);//当需要formatter的时候自己添加就可以了,原理就是拼接字符串.
                            });

                            if (exam.type == 1) {
                                columns.push({field: 'total', title: '总分', width: 70, sortable: true});

                                $("#escoreClazzList").combobox("readonly", false);

                                $("#escoreClazzList").combobox("clear");
                                $("#escoreClazzList").combobox("options").queryParams = {gradeid: exam.gradeid};
                                $("#escoreClazzList").combobox("reload");
                            } else {
                                $("#escoreClazzList").combobox("readonly", true);
                            }

                            $('#escoreList').datagrid({
                                columns: [
                                    columns
                                ]
                            });
                        }
                    });
                    setTimeout(function () {
                        $("#escoreList").datagrid("options").url = "ScoreServlet?method=ScoreList&t=" + new Date().getTime();
                        $("#escoreList").datagrid("options").queryParams = data;
                        $("#escoreList").datagrid("reload");

                        $("#escoreListDialog").dialog("open");
                    }, 100)
                }
            });

            //设置添加窗口
            $("#addDialog").dialog({
                title: "添加课程信息",
                width: 650,
                height: 460,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text: '添加',
                        plain: true,
                        iconCls: 'icon-add',
                        handler: function () {
                            var validate = $("#addForm").form("validate");
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据!", "warning");
                                return;
                            } else {
                                var courseid = $("#add_courseList").combobox("getValue");
                                var teacherid = $("#add_teacherList").combobox("getValue");
                                var clazzid = $("#add_clazzList").combobox("getValue");
                                $.ajax({
                                    type: "post",
                                    url: "/schedule/add",
                                    data: $("#addForm").serialize(),
                                    success: function (msg) {
                                        if (msg !== "error") {
                                            $.messager.alert("消息提醒", "添加成功!", "info");
                                            //关闭窗口
                                            $("#addDialog").dialog("close");
                                            //清空原表格数据
                                            // $("#add_name").textbox('setValue', "");
                                            // $("#add_time").datebox('setValue', "");
                                            // $("#add_remark").textbox('setValue', "");
                                            $("#add_courseList").combobox("clear");
                                            $("#add_teacherList").combobox("clear");
                                            $("#add_clazzList").combobox("clear");
                                            //重新加载年级
                                            // $("#add_gradeList").combobox("clear");
                                            // $("#add_gradeList").combobox("reload");

                                            //重新刷新页面数据
                                            // $("#gradeList").combobox('setValue', gradeid);
                                            $("#courseList").combobox('setValue', courseid);
                                            $("#teacherList").combobox('setValue', teacherid);
                                            $("#clazzList").combobox('setValue', clazzid);
                                            dataList.datagrid("options").queryParams = {
                                                courseid: courseid,
                                                teacherid: teacherid,
                                                clazzid: clazzid
                                            };
                                            dataList.datagrid("reload");

                                        } else {
                                            $.messager.alert("消息提醒", "添加失败!", "warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-reload',
                        handler: function () {
                            $("#add_name").textbox('setValue', "");
                            $("#add_time").datebox('setValue', "");
                            $("#add_remark").textbox('setValue', "");
                            //重新加载年级
                            $("#add_gradeList").combobox("clear");
                            $("#add_gradeList").combobox("reload");
                        }
                    },
                ]
            });

            //fengyexjtu@126.com
            //获取课程列表
            $("#courseList").combobox({
                width: "150",
                height: "25",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
                url: "/course/all",
                loadFilter: function (data) {
                    var obj = {};
                    obj.id = '';
                    obj.name = '-请选择-';
                    data.splice(0, 0, obj);//在数组0位置插入obj,不删除原来的元素
                    return data;
                }
            });

            //fengyexjtu@126.com
            //获取教师列表
            $("#teacherList").combobox({
                width: "150",
                height: "25",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
                url: "/teacher/all",
                loadFilter: function (data) {
                    var obj = {};
                    obj.id = '';
                    obj.name = '-请选择-';
                    data.splice(0, 0, obj);//在数组0位置插入obj,不删除原来的元素
                    return data;
                }
            });

            //fengyexjtu@126.com
            //获取 班级(clazz)列表
            $("#clazzList").combobox({
                width: "150",
                height: "25",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
                url: "/clazz/all",
                loadFilter: function (data) {
                    var obj = {};
                    obj.id = '';
                    obj.name = '-请选择-';
                    data.splice(0, 0, obj);//在数组0位置插入obj,不删除原来的元素
                    return data;
                }
            });

            //fengyexjtu@126.com
            //获取 班级(clazz)列表
            $("#add_clazzList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
                url: "/clazz/all",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });

            //fengyexjtu@126.com
            //获取 科目(course) 列表
            $("#add_courseList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
                url: "/course/all",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });

            //fengyexjtu@126.com
            //获取 教师(teacher)列表
            $("#add_teacherList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
                url: "/teacher/all",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });


            //考试成绩窗口
            $("#escoreListDialog").dialog({
                title: "成绩统计",
                width: 850,
                height: 550,
                iconCls: "icon-chart_bar",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                onClose: function () {
                    $("#escoreClazzList").combobox("clear");
                }
            });
            //成绩列表
            $('#escoreList').datagrid({
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                noheader: true,
                singleSelect: true,//是否单选
                rownumbers: true,//行号
                sortOrder: 'DESC',
                remoteSort: false,
                toolbar: "#escoreToolbar",
                frozenColumns: [[
                    {field: 'number', title: '学号', width: 120, resizable: false, sortable: false},
                    {field: 'name', title: '姓名', width: 120, resizable: false},
                ]],
            });

            $("#redo").click(function () {
                var exam = $("#dataList").datagrid("getSelected");
                var clazzid = exam.clazzid;
                if (exam.type == 1) {
                    clazzid = $("#escoreClazzList").combobox("getValue");
                }
                //var data = {id: exam.id, gradeid: exam.gradeid, clazzid:clazzid,courseid:exam.courseid, type: exam.type};

                var url = "ScoreServlet?method=ExportScore&id=" + exam.id + "&gradeid=" + exam.gradeid + "&clazzid=" + clazzid + "&courseid=" + exam.courseid + "&type=" + exam.type;

                window.open(url, "_blank");
            });

            //班级下拉框:选择成绩
            $("#escoreClazzList").combobox({
                width: "150",
                height: "25",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
                url: "/clazz/list",
                onChange: function (newValue, oldValue) {
                    var exam = $("#dataList").datagrid("getSelected");
                    var data = {
                        id: exam.id,
                        gradeid: exam.gradeid,
                        clazzid: newValue,
                        courseid: exam.courseid,
                        type: exam.type
                    };

                    $("#escoreList").datagrid("options").url = "ScoreServlet?method=ScoreList&t=" + new Date().getTime();
                    $("#escoreList").datagrid("options").queryParams = data;
                    $("#escoreList").datagrid("reload");
                }
            });

            // @fengyexjtu@126.com
            // searchButton 获取 clazzid,courseid,teacherid 的值,更新课程信息的数据
            $('#searchButton').bind('click', function () {
                // console.log("searchButton");
                var clazzid = $('#clazzList').combobox('getValue'); //班级
                var courseid = $('#courseList').combobox('getValue'); //课程
                var teacherid = $('#teacherList').combobox('getValue'); //教师
                console.log(clazzid, courseid, teacherid);
                dataList.datagrid("options").queryParams = {
                    clazzid: clazzid,
                    courseid: courseid,
                    teacherid: teacherid
                };
                dataList.datagrid("reload");
            });

        });
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>

<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-add',plain:true">添加或更细</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="delete" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="escore" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-chart_bar',plain:true">成绩统计</a></div>


    <div style="float: left; margin: 2px 10px;">班级：<input id="clazzList" class="easyui-textbox" name="clazz"/></div>
    <div style="float: left; margin: 2px 10px;">科目：<input id="courseList" class="easyui-textbox" name="course"/></div>
    <div style="float: left; margin: 2px 10px;">教师：<input id="teacherList" class="easyui-textbox" name="teacher"/></div>
    <div style="float: left; margin: 2px 10px;"><a href="#" id="searchButton" class="easyui-linkbutton dsbtn"
                                                   iconCls="icon-search">查询</a></div>
    <div style="clear: both"></div>
</div>

<!-- 考试成绩表 -->
<div id="escoreListDialog">
    <table id="escoreList" cellspacing="0" cellpadding="0">

    </table>
</div>
<div id="escoreToolbar">
    <a id="redo" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true">导出</a>
    <span style="margin-left:10px;">班级：<input id="escoreClazzList" class="easyui-textbox" name="clazz"/></span>
</div>


<!-- 添加窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8">
            <!--<tr>-->
            <!--<td>名称:</td>-->
            <!--<td><input id="add_name" class="easyui-textbox" style="width: 200px; height: 30px;" type="text"-->
            <!--name="name" data-options="required:true, missingMessage:'请输入名称'"/></td>-->
            <!--</tr>-->
            <!--<tr>-->
            <!--<td>考试时间:</td>-->
            <!--<td><input id="add_time" style="width: 200px; height: 30px;" class="easyui-datebox" type="text"-->
            <!--name="etime" data-options="required:true, missingMessage:'请选择日期', editable:false"/></td>-->
            <!--</tr>-->
            <!--<tr>-->
            <!--<td>班级:</td>-->
            <!--<td>-->
            <!--<input style="width: 200px; height: 30px;" class="easyui-textbox" data-options="readonly: true"-->
            <!--type="text" value="年级统考"/>-->
            <!--<input type="hidden" name="type" value="1"/>-->
            <!--</td>-->
            <!--</tr>-->
            <tr>
                <td>班级:</td>
                <td><input id="add_clazzList" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="clazzid"/></td>
            </tr>
            <tr>
                <td>科目:</td>
                <td><input id="add_courseList" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="courseid"/></td>
            </tr>
            <tr>
                <td>教师:</td>
                <td><input id="add_teacherList" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="teacherid"/></td>
            </tr>
            <!--<tr>-->
            <!--<td>备注:</td>-->
            <!--<td><input id="add_remark" style="width: 200px; height: 70px;" class="easyui-textbox"-->
            <!--data-options="multiline: true," name="remark"/></td>-->
            <!--</tr>-->
        </table>
    </form>
</div>

</body>
</html>